package cn.tedu.yushouge.pet.adopt.controller;

import cn.tedu.yushouge.commons.pojo.vo.PageData;
import cn.tedu.yushouge.pet.adopt.pojo.param.AdoptAddNewParam;
import cn.tedu.yushouge.pet.adopt.pojo.param.AdoptUpdateInfoParam;
import cn.tedu.yushouge.pet.adopt.pojo.vo.AdoptListItemVO;
import cn.tedu.yushouge.pet.adopt.pojo.vo.AdoptListVO;
import cn.tedu.yushouge.pet.adopt.pojo.vo.AdoptStandardVO;
import cn.tedu.yushouge.pet.adopt.service.IAdoptService;
import cn.tedu.yushouge.commons.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;


@RestController
@Api(tags = "01. 宠物领养管理模块")
@RequestMapping("/adopt/")
@Slf4j
@Validated
public class AdoptController {


    @Autowired
    IAdoptService adoptService;

    //http://localhost:9818/pet/add-new
    @PostMapping("add-new")
    @ApiOperation("添加宠物领养申请")
    @ApiOperationSupport(order = 100)
    public JsonResult<Void> addNew(@Valid AdoptAddNewParam adoptAddNewParam) {
        log.debug("开始处理【添加宠物领养申请】的请求，参数：{}", adoptAddNewParam);
        adoptService.addNew(adoptAddNewParam);
        return JsonResult.ok();
    }
    //http://localhost:9818/pet/delete
    @PostMapping("delete")
    @ApiOperation("根据ID删除宠物领养申请")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "宠物ID", required = true, dataType = "long")
    })
    public JsonResult<Void> delete(@Range(min = 1, message = "根据ID删除宠物失败，请提交合法的ID值！")
                             @RequestParam Long id) {
        log.debug("开始处理【根据ID删除宠物领养申请】的请求，参数：{}", id);
        adoptService.deleteById(id);
        return JsonResult.ok();
    }

    //http://localhost:9818/pet/update
    @PostMapping("/update")
    @ApiOperation("修改宠物领养申请详情")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "品牌ID", required = true, dataType = "long")
    })
    public JsonResult<Void> updateInfoById(@RequestParam @Range(min = 1, message = "请提交有效的宠物ID值!") Long id,
                                     @Valid AdoptUpdateInfoParam adoptUpdateInfoParam) {
        log.debug("开始处理【修改宠物领养申请详情】的业务，参数：{}",adoptUpdateInfoParam);
        adoptService.updateInfoById(id,adoptUpdateInfoParam);
        return JsonResult.ok();
    }

    @PostMapping("/update-agree")
    @ApiOperation("同意领养申请")
    @ApiOperationSupport(order = 310)
    public JsonResult<Void> agreeAdopt(@RequestParam @Range(min = 1, message = "请提交有效的宠物ID值!") Long id) {
        log.debug("开始处理【修改同意宠物领养申请详情】的业务，ID:{}",id);
        adoptService.agreeAdoptById(id);
        return JsonResult.ok();
    }

    @PostMapping("/update-disAgree")
    @ApiOperation("不同意领养申请")
    @ApiOperationSupport(order = 310)
    public JsonResult<Void> disAgreeAdopt(@RequestParam @Range(min = 1, message = "请提交有效的宠物ID值!") Long id,String remark) {
        log.debug("开始处理【修改不同意宠物领养申请详情】的业务，ID:{},原因:{}",id,remark);
        adoptService.disAgreeAdoptById(id,remark);
        return JsonResult.ok();
    }

    //http://localhost:9818/pet/standard
    @GetMapping("standard")
    @ApiOperation("根据ID查询宠物领养申请详情")
    @ApiOperationSupport(order = 410)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "订单ID", required = true, dataType = "long")
    })
    public JsonResult<AdoptStandardVO> getStandardById(@Range(min = 1, message = "根据ID查询宠物详情失败，请提交合法的ID值！")
                                      @RequestParam Long id) throws Exception {
        log.debug("开始处理【根据ID查询宠物领养申请详情】的请求，参数：{}", id);
        AdoptStandardVO result = adoptService.getStandardById(id);
        return JsonResult.ok(result);
    }

    //http://localhost:9818/pet/list
    @GetMapping("list")
    @ApiOperation("查询宠物领养申请")
    @ApiOperationSupport(order = 420)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", paramType = "query")
    })
    public JsonResult<PageData<AdoptListVO>> list(@Range(min = 1, message = "查询宠物失败，请提供正确的页码值！") Integer pageNum) {
        log.debug("开始处理【查询宠物领养列表申请】的请求，页码：{}", pageNum);
        if (pageNum == null || pageNum < 1) {
            pageNum = 1;
        }
        PageData<AdoptListVO> pageData = adoptService.list(pageNum);
        return JsonResult.ok(pageData);
    }

}
